// components/surveyresultanalysis/index.js
import * as echarts from '../../lib/ec-canvas/echarts';

let chart = null;

let defaultOption = {
  tooltip: {
    trigger: 'axis'
  },
  radar: {
    indicator: [{ text: '评分', max: 0 }],
    radius: 75,
    name: {
      fontSize: 16,
      rich: {}
    }
  },
  series: [
    {
      type: 'radar',
      // tooltip: {
      //   trigger: 'item'
      // },
      // lineStyle: {
      //   color: "#0e2b47"
      // },
      itemStyle: {
        // color: "#0e2b47",
        normal: {
          color: "#0e2b47",
          areaStyle: {
            type: 'default'
          }
        }
      },
      data: [
        {
          value: [0],
          name: '评分'
        }
      ]
    }
  ]
};

function initChart(canvas, width, height) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  chart.setOption(defaultOption);
  return chart;
}

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    surveyName: {
      type: String
    },
    userSurveyScoreVos: {
      type: Array
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    sumScore: "",
    ec: {
      onInit: initChart
    },
    isEchartsShow: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    ok(e) {
      this.triggerEvent('ok', e.detail);
    },
    cancel(e) {
      this.triggerEvent('cancel', e.detail);
    },
    init() {
      let _that = this;
      let sumScore = 0;
      if (_that.properties.userSurveyScoreVos.length > 2) {
        let indicatorArr = _that.properties.userSurveyScoreVos.map((item, index, arr) => {
          return {
            text: item.dimensionalityVo.name, 
            max: item.hundredMarkMaxScore
          };
        });
        let valueArr = _that.properties.userSurveyScoreVos.map((item, index, arr) => {
          sumScore += item.hundredMarkScore;
          return item.hundredMarkScore;
        });
        _that.changeChartData(indicatorArr, valueArr);
        _that.setData({
          sumScore: sumScore.toFixed(0)
        });
      } else {
        let valueArr = _that.properties.userSurveyScoreVos.map((item, index, arr) => {
          sumScore += item.hundredMarkScore;
          return item.hundredMarkScore;
        });
        _that.setData({
          sumScore: sumScore.toFixed(0),
          isEchartsShow: false
        });
      }
    },
    /**
     * 改变echart的数据
     */
    changeChartData(indicator, value) {
      let _that = this;
      let option = defaultOption;
      option.radar.indicator = indicator;
      option.series[0].data[0].value = value;
      // let option = {
      //   radar: {
      //     indicator,
      //     radius: 80,
      //     name: {
      //       fontSize: 180
      //     }
      //   },
      //   series: [
      //     {
      //       data: [
      //         {
      //           value
      //         }
      //       ]
      //     }
      //   ]
      // };
      let interval = setInterval(() => {
        console.log("等待echarts渲染");
        if (chart && chart.setOption) {
          console.log("echarts渲染成功");
          chart.setOption(option);
          clearInterval(interval);
        }
      }, 300);
    }
  },

  ready() {
    let _that = this;
    _that.init();
  },
})
